﻿using System;
using TVT.Common.Helpers;
using TVT.DataAccess.Repositories;
using System.Collections.Generic;
namespace TVT.BizLayer.Entities
{
    [Serializable]
    public class UserAlias : BaseEntity<UserAlias>
    {

        #region attributes
        
        int id;
        string name;
        Guid idUser;
        int idPlatform;

        private static DBRepository repository = DBRepository.GetInstance();
                
        #endregion
        
        #region properties

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Guid IdUser
        {
            get { return idUser; }
            set { idUser = value; }
        }

        public Enumerators.Platform Platform
        {
            get { 
                return (Enumerators.Platform)Enum.ToObject(typeof(Enumerators.Platform) ,idPlatform);
            }
            set { idPlatform = (int)value; }
        }


        public int IdPlatform
        {
            get { return idPlatform; }
            set { idPlatform = value; }
        }

        #endregion

        #region constructor
        public UserAlias(string name, Guid idUser, Enumerators.Platform platform)
        {
            this.Name = name;
            this.IdUser = IdUser;
            this.Platform = platform;

        }

        public UserAlias() { }
        #endregion

        #region BaseEntity methods
        public override void Insert()
        {
            this.id = repository.InsertUserAlias(name, idUser, idPlatform);
        }

        public override void Update()
        {
            throw new System.NotImplementedException();
        }

        public override void Delete()
        {
            throw new System.NotImplementedException();
        }

        public override void Save()
        {
            if (id == 0)
                Insert();
            else
                Update();
        }
        #endregion

        #region public methods

        public static List<UserAlias> FindAllByUser(Guid idUser)
        {
            return MapToEntities(repository.FindAllUserAliasByIdUser(idUser));
        }
        public static void DeleteAllByUser(Guid idUser)
        {
            repository.DeleteAllUserAliasByIdUser(idUser);
        }

        public string GetKey()
        {
            return this.Id + ";" + this.Name + ";" + this.IdUser + ";" + this.Platform.ToString();
        }

        public static string MakeKey( int id, string name, Guid idUser, Enumerators.Platform platform )
        {
            return id + ";" + name + ";" + idUser + ";" + platform.ToString();
        }

        public override bool Equals(object obj)
        {
            UserAlias objAlias = (UserAlias)obj;

            return (objAlias.GetKey() == this.GetKey());

        }
                
        #endregion


        
    }
}